/**
 * Created by skiy on 16-6-16.
 */
$(function () {
    $(document).ready(function () {
        loadSayer();
        getNumber();
    });


    //浏览器缩放监视
    $(window).resize(function() {
        loadSayer();  //调整用户心声头像
    });

    //首页轮播大图
    var indexSwiper;
    indexSwiper = new Swiper('.index-swiper-container', {
        autoplay: 5000,
        pagination: '.index-swiper-pagination',
        //swipeToNext: '.swiper-button-next',
        //swipeToPrev: '.swiper-button-prev',
        paginationClickable: true,
        preloadImages: false,
        loop: true,
        //lazyLoading: true,
        keyboardControl: true
    });

    //下一页
    $(".swiper-button-next").click(function(){
        indexSwiper.swipeNext();
    });

    //上一页
    $(".swiper-button-prev").click(function(){
        indexSwiper.swipePrev();
    });

    /**
     * 根据窗口大小决定“用户心声”要显示slide个数
     * @returns 要显示slide个数
     */
    function changeSlidePV()
    {
        var w_windth = $(window).width(),slides = 1;
        if(w_windth > 1200)
        {
            slides = 3;
        }
        else if(w_windth > 992)
        {
            slides = 2;
        }
        else
        {
            slides = 1;
        }
        return slides;
    }

    //用户心声
    var homeSayer;
    homeSayer = new Swiper('.home-sayer-container', {
        autoplay: 4000,
        pagination: '.home-sayer-pagination',
        //pagination: '.swiper-pagination',
        centeredSlides: true,

        watchActiveIndex: true,

        slidesPerView: changeSlidePV(),

        autoResize: true,

        paginationClickable: true,

        initialSlide: 3,

        loop: true,

        keyboardControl: true,
    });

    $(window).resize(function(){
        //窗口更改，用户心声模块swiper需重新reload
        //window.location.reload();
    });


    //查阅次数统计滚动
    $('.banner-read-more').click(function () {
        $("body").scrollTo($(".home_protect"), 200);
    });

    //产品页滚动定位
    $(".products_nav li").click(function() {
        var idx = $(this).index() + 1;
        var obj = $(".scroll_" + idx);

        $("body").scrollTo(obj, 200);
    });

    //保护无处不在选中
    $(".container-h-protect-btn").mouseover(function(){
        $(".container-h-protect-btn").removeClass("container-h-protect-select");
        $(this).addClass("container-h-protect-select");

        $(".home_protect_list_item").eq(0).stop();
        $(".home_protect_list_item").eq(1).stop();
        $(".home_protect_list_item").eq(2).stop();

        var idx = $(this).index();
        var sec = 150;

        if(idx == 0)
        {
            $(".home_protect_list_item").eq(0).animate({
                marginLeft: 0
            }, sec);

            $(".home_protect_list_item").eq(1).animate({
                marginLeft: "224px"
            }, sec);

            $(".home_protect_list_item").eq(2).animate({
                marginLeft: "448px"
            }, sec);
        }

        if(idx == 1)
        {
            $(".home_protect_list_item").eq(0).animate({
                marginLeft: "-224px"
            }, sec);

            $(".home_protect_list_item").eq(1).animate({
                marginLeft: 0
            }, sec);

            $(".home_protect_list_item").eq(2).animate({
                marginLeft: "224px"
            }, sec);
        }

        if(idx == 2)
        {
            $(".home_protect_list_item").eq(0).animate({
                marginLeft: "-448px"
            }, sec);

            $(".home_protect_list_item").eq(1).animate({
                marginLeft: "-224px"
            }, sec);

            $(".home_protect_list_item").eq(2).animate({
                marginLeft: 0
            }, sec);
        }
    });

    //首页解决方案
    $('.resolve_description').mouseover(function () {
        //所有操作停止
        $(this).find('.resolve_level_1 > div').stop();
        //移开现有层
        $(this).find('.resolve_level_1 > .desc_1').animate({
            top: -311
        }, 50);
        //更改透明度
        $(this).find('.resolve_level_1 > .resolve_mask').animate({
            opacity: 0.9
        });
        //替换当前层
        $(this).find('.resolve_level_1 > .desc_2').animate({
            top : 0
        }, 50);

    }).mouseleave(function () {
        $(this).find('.resolve_level_1 > div').stop();
        //移开现有层
        $(this).find('.resolve_level_1 > .desc_2').animate({
            top: 311
        })
        //更改透明度
        $(this).find('.resolve_level_1 > .resolve_mask').animate({
            opacity: 0.6
        });
        //替换当前层
        $(this).find('.resolve_level_1 > .desc_1').animate({
            top : 0
        });
    });

    /**
     * 用户心声头像居中
     */
    function loadSayer() {
        var said_level_width = $(".home_sayer .said_level_1:first").width()
        var sayer_avatar_point = said_level_width / 2 - 40;
        $(".home_sayer .home-sayer-avatar").css('left', sayer_avatar_point);
    }

    /**
     * 获取文件查阅数、文件保护数及用户数
     */
    function getNumber() {
        var tsObj = $('.home_controller_stat');
        var ts1 = tsObj.data('ts');
        var ts2 = ts1 + 60 * 1000;

        var timestamp = new Date().getTime();

        if (timestamp < ts2) {
            timestamp = ts2;
        }

        $.getJSON('/home/protect_stat?_ts=' + timestamp, function(data){

            if(data.error_code == 0){
                $("#index_viewNumber").html(data.data.view);
                $("#index_protectNumber").html(data.data.protect);
                $("#index_userNumber").html(data.data.user);

                tsObj.data('ts');
            }
        });
    }

    //5秒跳动一次
    var simuIP = window.setInterval(function() {
        getNumber();
    }, 30000);
});